package com.ssy.lingxi.member.merchant.serviceimpl.queue;

import com.ssy.lingxi.common.model.dto.UserLoginCacheDTO;
import com.ssy.lingxi.common.utils.ObjectSerializableUtil;
import com.ssy.lingxi.logistics.api.constans.LogisticsServiceConstants;
import com.ssy.lingxi.logistics.api.dto.queue.LogisticsMessageQueueDTO;
import com.ssy.lingxi.logistics.api.dto.request.*;
import com.ssy.lingxi.logistics.api.enums.LogisticsMessageQueueTypeEnum;
import com.ssy.lingxi.member.merchant.entity.MemberStoreDO;
import com.ssy.lingxi.member.merchant.model.vo.maintenance.request.MemberDeliverAddressVO;
import com.ssy.lingxi.member.merchant.model.vo.maintenance.request.MemberReceiveAddressVO;
import com.ssy.lingxi.member.merchant.service.queue.ILogisticsMessageQueueService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 内部物流服务消息队列实现类
 * @author wyx
 * @version 2.0.0
 * @date 2022-03-10
 **/
@Service
public class LogisticsMessageQueueServiceImpl implements ILogisticsMessageQueueService {

    @Resource
    private RabbitTemplate rabbitTemplate;


    /**
     * 同步门店收发货地址数据到物流服务
     * @param memberStoreDO 门店信息
     * @param loginUser     登录用户信息
     * @param deliverAddress 发货地址
     * @param receiveAddress 收货地址
     * @param isCreate       是否为新增地址
     */
    @Override
    public void syncLogisticsAddress(MemberStoreDO memberStoreDO, UserLoginCacheDTO loginUser, MemberDeliverAddressVO deliverAddress, MemberReceiveAddressVO receiveAddress, Boolean isCreate) {
        if (isCreate && deliverAddress == null && receiveAddress == null) {
            return;
        }

        LogisticsAddressSyncQueueVO queueVO = new LogisticsAddressSyncQueueVO();
        queueVO.setStoreId(memberStoreDO.getId());
        queueVO.setStoreStatus(memberStoreDO.getStatus());
        queueVO.setStoreName(memberStoreDO.getName());
        queueVO.setMemberId(loginUser.getMemberId());
        queueVO.setMemberRoleId(loginUser.getMemberRoleId());

        //发货地址
        if (deliverAddress != null) {
            LogisticsDeliverAddress logisticsDeliverAddress = new LogisticsDeliverAddress();
            logisticsDeliverAddress.setShipperName(deliverAddress.getDeliverName());
            logisticsDeliverAddress.setProvinceCode(deliverAddress.getProvinceCode());
            logisticsDeliverAddress.setProvinceName(deliverAddress.getProvinceName());
            logisticsDeliverAddress.setCityCode(deliverAddress.getCityCode());
            logisticsDeliverAddress.setCityName(deliverAddress.getCityName());
            logisticsDeliverAddress.setDistrictCode(deliverAddress.getDistrictCode());
            logisticsDeliverAddress.setDistrictName(deliverAddress.getDistrictName());
            logisticsDeliverAddress.setStreetCode(deliverAddress.getStreetCode());
            logisticsDeliverAddress.setStreetName(deliverAddress.getStreetName());
            logisticsDeliverAddress.setAddress(deliverAddress.getAddress());
            logisticsDeliverAddress.setPostalCode(deliverAddress.getPostalCode());
            logisticsDeliverAddress.setAreaCode(deliverAddress.getAreaCode());
            logisticsDeliverAddress.setPhone(deliverAddress.getPhone());
            logisticsDeliverAddress.setTel(deliverAddress.getTel());
            logisticsDeliverAddress.setIsDefault(deliverAddress.getIsDefault());
            queueVO.setLogisticsDeliverAddress(logisticsDeliverAddress);
        }

        //收货地址
        if (receiveAddress != null) {
            LogisticsReceiveAddress logisticsReceiveAddress = new LogisticsReceiveAddress();
            logisticsReceiveAddress.setReceiverName(receiveAddress.getReceiverName());
            logisticsReceiveAddress.setProvinceCode(receiveAddress.getProvinceCode());
            logisticsReceiveAddress.setProvinceName(receiveAddress.getProvinceName());
            logisticsReceiveAddress.setCityCode(receiveAddress.getCityCode());
            logisticsReceiveAddress.setCityName(receiveAddress.getCityName());
            logisticsReceiveAddress.setDistrictCode(receiveAddress.getDistrictCode());
            logisticsReceiveAddress.setDistrictName(receiveAddress.getDistrictName());
            logisticsReceiveAddress.setStreetCode(receiveAddress.getStreetCode());
            logisticsReceiveAddress.setStreetName(receiveAddress.getStreetName());
            logisticsReceiveAddress.setAddress(receiveAddress.getAddress());
            logisticsReceiveAddress.setPostalCode(receiveAddress.getPostalCode());
            logisticsReceiveAddress.setAreaCode(receiveAddress.getAreaCode());
            logisticsReceiveAddress.setPhone(receiveAddress.getPhone());
            logisticsReceiveAddress.setTel(receiveAddress.getTel());
            logisticsReceiveAddress.setIsDefault(receiveAddress.getIsDefault());
            queueVO.setLogisticsReceiveAddress(logisticsReceiveAddress);
        }


        LogisticsMessageQueueDTO messageQueueDTO = new LogisticsMessageQueueDTO();
        messageQueueDTO.setType(LogisticsMessageQueueTypeEnum.SYNC_LOGISTICS_ADDRESS.getCode());
        messageQueueDTO.setMessage(ObjectSerializableUtil.serialize(queueVO));

        rabbitTemplate.convertAndSend(LogisticsServiceConstants.LOGISTICS_SERVICE_EXCHANGE, LogisticsServiceConstants.LOGISTICS_SERVICE_ROUTING_KEY, ObjectSerializableUtil.serialize(messageQueueDTO));
    }

    /**
     * 删除门店同步删除物流服务地址数据
     * @param memberStoreDO 门店信息
     * @param loginUser     登录用户信息
     */
    @Override
    public void deleteLogisticsAddress(MemberStoreDO memberStoreDO, UserLoginCacheDTO loginUser) {
        LogisticsMessageQueueDTO messageQueueDTO = new LogisticsMessageQueueDTO();
        LogisticsAddressDeleteQueueVO queueVO = new LogisticsAddressDeleteQueueVO();
        queueVO.setMemberId(loginUser.getMemberId());
        queueVO.setMemberRoleId(loginUser.getMemberRoleId());
        queueVO.setStoreId(memberStoreDO.getId());
        messageQueueDTO.setType(LogisticsMessageQueueTypeEnum.DELETE_LOGISTICS_ADDRESS.getCode());
        messageQueueDTO.setMessage(ObjectSerializableUtil.serialize(queueVO));

        rabbitTemplate.convertAndSend(LogisticsServiceConstants.LOGISTICS_SERVICE_EXCHANGE, LogisticsServiceConstants.LOGISTICS_SERVICE_ROUTING_KEY, ObjectSerializableUtil.serialize(messageQueueDTO));
    }

    /**
     * 启用/停用门店同步更新物流服务地址状态数据
     * @param memberStoreDO 门店信息
     * @param loginUser     登录用户信息
     */
    @Override
    public void updateLogisticsAddressStatus(MemberStoreDO memberStoreDO, UserLoginCacheDTO loginUser) {
        LogisticsMessageQueueDTO messageQueueDTO = new LogisticsMessageQueueDTO();
        LogisticsAddressUpdateQueueVO queueVO = new LogisticsAddressUpdateQueueVO();
        queueVO.setMemberId(loginUser.getMemberId());
        queueVO.setMemberRoleId(loginUser.getMemberRoleId());
        queueVO.setStoreId(memberStoreDO.getId());
        queueVO.setStatus(memberStoreDO.getStatus());
        messageQueueDTO.setType(LogisticsMessageQueueTypeEnum.UPDATE_LOGISTICS_ADDRESS_STATUS.getCode());
        messageQueueDTO.setMessage(ObjectSerializableUtil.serialize(queueVO));

        rabbitTemplate.convertAndSend(LogisticsServiceConstants.LOGISTICS_SERVICE_EXCHANGE, LogisticsServiceConstants.LOGISTICS_SERVICE_ROUTING_KEY, ObjectSerializableUtil.serialize(messageQueueDTO));
    }
}
